package com.mytest.tomcat;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import java.io.File;

public class SpringApplication {

    public static void run(){
        try {
            //1、创建tomcat服务器
            Tomcat tomcat = new Tomcat();
            //指定端口
            tomcat.setPort(8080);
            //指定静态资源路径
            //必须指定静态路径，否则tomcat无法扫描到SpringServletContainerInitializer
            StandardContext ctx = (StandardContext) tomcat.addWebapp("/",new File("src/main").getAbsolutePath());
            // 禁止重新载入
            ctx.setReloadable(false);
            // class文件地址
            File file = new File("target/classes");
            // 创建webRoot
            WebResourceRoot resource = new StandardRoot(ctx);
            // Tomcat内部读取class执行
            resource.addJarResources(new DirResourceSet(resource,"/WEB-INF/classes",file.getAbsolutePath(),"/"));
            // 启动Tomcat
            tomcat.start();
            System.out.println("Tomcat服务启动成功...");
            // 异步接收请求
            tomcat.getServer().await();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
